﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SernaJson;
using Newtonsoft.Json;
using System.Linq.Expressions;

namespace VisualTester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class MccServer
        {

            /// <summary>
            /// Server address including protocol://address(named or IP):port
            /// </summary>

            public string ExternalHost = null;
            /// <summary>
            /// available slots for users/companies. Default capacity: 2000 per server
            /// </summary>
            
            public ushort FreeSlots = 0;
            /// <summary>
            /// Server address including protocol://address(named or IP):port
            /// </summary>
            
            public string InternalHost = string.Empty;

            /// <summary>
            /// Server ID
            /// </summary>
            
            public long Id = -1;

            /// <summary>
            /// Count of the customer bound to this host
            /// </summary>
            
            public long CustomersCount = 0;

            /// <summary>
            /// Means that to this server can be bound customers from outside automatically.
            /// If false, only manual binding of customers is allowed
            /// </summary>
            
            public bool AutomaticServer = true;

            public DateTime dt = new DateTime(2012, 12, 4, 12, 47, 45);
        }

        public class TType1
        {
            public long P1 = 12;
            public float P2 = 4.5f;
            public int? P4 = null;
            public Dictionary<int, MTTR> df = new Dictionary<int, MTTR>();
            //public Tuple<int, int> tz = new Tuple<int, int>(1,2);
        }


        [ProtoBuf.ProtoContract]
        public class MccUserData
        {
            public MccUserData()
            {               
                //CarMagicAppLoggingPermission = true;
                //CultureId = 1031;
                //IsCaMagicLoggerAppSubscriber = false;
                //SubscribedApps = new HashSet<string>();
                //CompanyBindingId = String.Empty;

                //IDoThings_Enabled = false;
                //IDoThings_ExpirationDate = DateTime.MinValue;
                //IDoThings_PlayStore = false;
                //FloaTest = 12.45f;
                //DoubleTest = 13.45;
                //DecimalTest = 13.45m;
                //rD = new Dictionary<int, string>();
                //rD.Add(1, "sdfsdf");
                //rD.Add(2, "asdsf");

                //rObj = new Dictionary<int, TType1>();
                //rObj.Add(1, new TType1());
                //rObj.Add(2, new TType1());

                //arr = new int?[] { 1, 3, null,4 };

                //sdt = new List<string> { "asd", "sdfds", null, "sdff" };

                //TestString = "sdfsd\thkhkj\n\tsfsdfsdf";
                //TestEnum = eTestE.Two;
                //tt1 = new TType1();
                //Active = true;
                //CharTest = '\t';

                //TestByte = -42;

                //TestTimeSpan = new TimeSpan(0,4, 3, 10,125);

                //TestGuid = Guid.NewGuid();
                //arr1 = new int[2][];
                //arr1[0] = new int[3];
                //arr1[0][1] = 2;

                //ifjhklfdfjlkdjfldgfdgdgdfgdgdgdf = 54;
                //str = " dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh";
                //str = " dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl\t l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh";
                //str = " dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh dsjfh ksdhksjdh fksdh fljsahlkfj hsaldkjfh laskdjfhl l jaksdhklfhj saldfjhsldkjfh laksjd lsdflkashdlf sadlkfjh lsdkf hld flkasdh flkasdllsd lasd  dsfhalsd hlasdjkh flsdkj lsdajhf laskjdh lsd lksjdfh ksjdhf ksdj ksdj lsajkdh flajksd lksdjfh";
                //str = "d";
                //str1 = " dsfgh fgh fghfs sd fsdf sdf sfsfsdfsdfsdfsd dfgd fdf df dffs gh fgh";
                //str2 = " dsfgh fgh fsdfsdfsdfghfs dfsfsdfsd sdf sdf sdfs gh dfdfg df df  fgh";

                //i1 = 324;
                //i2 = 324;
                //i3 = 324;
                //i4 = 324;

                s1 = "a";
                s2 = "a";
                s3 = "a";
                s4 = "a";
                s5 = "a";
            }

            //public int ifjhklfdfjlkdjfldgfdgdgdfgdgdgdf = 0;

            //[ProtoBuf.ProtoMember(1, IsRequired = true)]
            //public string str { get; set; }


            //public int i1 { get; set; }
            //public int i2 { get; set; }
            //public int i3 { get; set; }
            //public int i4 { get; set; }

            public string s1 { get; set; }
            public string s2 { get; set; }
            public string s3 { get; set; }
            public string s4 { get; set; }
            public string s5 { get; set; }

            //public string str1 { get; set; }
            //public string str2 { get; set; }

            public enum eTestE
            {
                One = 1,
                Two = 2,
                Three = 1,
            }

            public eTestE TestEnum { get; set; }

            public TType1 tt1 { get; set; }

            public long CustomerId { get; set; }

            public long HostId { get; set; }

            public string Login { get; set; }

            public bool Active { get; set; }

            public DateTime DtuCreated { get; set; }

            public DateTime ExpirationDate { get; set; }
            public DateTime? ExpirationDate1 { get; set; }

            public bool LoggingPermission { get; set; }


            public string Language { get; set; }

            public int CultureId { get; set; }

            public bool IsAppSubscriber { get; set; }

            public byte[] RowKey { get; set; }

            public HashSet<string> SubscribedApps { get; set; }


            public string CompanyBindingId { get; set; }

            public bool IDT_Enabled { get; set; }

            public DateTime IDT_ExpirationDate { get; set; }

            public bool IDT_PlayStore { get; set; }

            public float FloaTest { get; set; }
            public double DoubleTest { get; set; }
            public decimal DecimalTest { get; set; }
            public double? DoubleNullTest { get; set; }
            public char CharTest { get; set; }

            [ProtoBuf.ProtoMember(2, IsRequired = true)]
            public Dictionary<int, string> rD { get; set; }

            public Dictionary<int, TType1> rObj { get; set; }

            ////public int?[] arr { get; set; }

            public List<string> sdt { get; set; }

            public string TestString { get; set; }

            public sbyte TestByte { get; set; }

            public TimeSpan TestTimeSpan { get; set; }

            public Guid TestGuid { get; set; }

            //public int[][] arr1 { get; set; }
        }

        public class MTTR
        {          
            public Guid TestGuid { get; set; }
        }

        public class MTTR1
        {
            public MTTR1()
            {
                TestTuple = new Tuple<int, DateTime, string, Tuple<double, List<string>>>(1, DateTime.Now, "xisbound",
                    new Tuple<double, List<string>>(45.45, new List<string> { "hi", "man" }));
            }
            public Tuple<int,DateTime,string,Tuple<double,List<string>>> TestTuple { get; set; }
        }
       
        private void button1_Click(object sender, EventArgs e)
        {

            var dd = new Dictionary<string, string> { { "Result", "OK" }, { "Stone", "Bazalt" }, { "Chok", "Well" }, };
            var rr = Serna.Serializers.SerializeToSernaString(dd,new Serna.SerializationSettings() { Framing = '\"', Delimiter = ',' });

            return;
            MccUserData muj1 = MujInst();

            NetJSON.NetJSON.IncludeFields = true;       //Default value
            MTTR1 sdf = new MTTR1();
            string serinst = NetJSON.NetJSON.Serialize(sdf);
            var desinst = NetJSON.NetJSON.Deserialize<MTTR1>(serinst);
            return;

            //var s = System.IO.File.ReadAllText(@"d:\ser3.txt");
            //var retxx=NetJSON.NetJSON.Deserialize<Dictionary<string, string>>(s);
            //return;
            //var retxx = Jil.JSON.Deserialize<Dictionary<string, string>>(System.IO.File.ReadAllText(@"d:\ser3.txt"));

            //CapnProto.Message.Load( msf = new CapnProto.Message();

            string ax = muj1.SerializeToJsonString();
            //string jils = Jil.JSON.Serialize(muj1);
            NetJSON.NetJSON.IncludeFields = true;       //Default value
            string njs = NetJSON.NetJSON.Serialize(muj1);

            //string ssx = ServiceStack.Text.JsonSerializer.SerializeToString(muj1);

            //var serializer = MsgPack.Serialization.MessagePackSerializer.Get<MccUserData>();
            //// 2. Serialize object to the specified stream.
            //byte[] sd = serializer.PackSingleObject(muj1);
            //byte[] prt = muj1.SerializeProtobuf();
            //var rezt = serializer.UnpackSingleObject(sd);

            return;

            ////string jsonx = JsonConvert.SerializeObject(muj1);
            ////string fastx = fastJSON.JSON.ToJSON(muj1, new fastJSON.JSONParameters() { UsingGlobalTypes = false, });
            ////string ssx = ServiceStack.Text.JsonSerializer.SerializeToString(muj1);          //https://github.com/ServiceStack/ServiceStack.Text/blob/master/src/ServiceStack.Text/Json/JsonTypeSerializer.cs                    

            
            ////string msx = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(muj1);

            //return;

            //var retxx = fastJSON.JSON.ToObject<Dictionary<string, string>>(System.IO.File.ReadAllText(@"d:\ser3.txt"));
            //var retxx = JsonConvert.DeserializeObject<Dictionary<string, string>>(System.IO.File.ReadAllText(@"d:\ser3.txt"));
            //var retxx = System.IO.File.ReadAllText(@"d:\ser3.txt").DeserializeFromJsonStringToDictionary();

           
            return;
            
        }

        private MccUserData MujInst()
        {
            MccUserData muj1 = new MccUserData()
            {

            };

            muj1.rD = new Dictionary<int, string>();
            muj1.rD.Add(1, "sdfsdf");
            muj1.rD.Add(2, "asdsf");

            muj1.rObj = new Dictionary<int, TType1>();
            muj1.rObj.Add(1, new TType1());
            muj1.rObj.Add(2, new TType1());

            //muj1.DtuCreated = new DateTime(2015, 1, 7, 12, 17, 0, 188, DateTimeKind.Utc);
            //muj1.ExpirationDate = new DateTime(2012, 1, 7, 12, 17, 0);
            //muj1.RowKey = new byte[] { 12, 1, 5, 3 };
            //muj1.ExpirationDate = new DateTime(2015, 2, 12, 10, 15, 12, 245, DateTimeKind.Utc);
            //muj1.SubscribedApps.Add("wkk");
            //muj1.SubscribedApps.Add("sdfsd");
            muj1.CharTest = 'n';
            //muj1.DoubleTest *= 15645684432432.0;
            muj1.DoubleTest *= 1564568442.0;
            muj1.DecimalTest *= 15645684432432.0m;

            return muj1;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MccUserData muj1 = MujInst();
            MccUserData desMuj1 = null;
           // TType1 muj1 = new TType1();

            byte[] btJson = null;
            string json = "";

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

            ////json = fastJSON.JSON.ToJSON(muj1);
            ////sw.Start();
            ////for (int i = 0; i < 100000; i++)
            ////{
            ////    json = fastJSON.JSON.ToJSON(muj1);
            ////}
            ////sw.Stop();
            ////Console.WriteLine("FastJSON: " + sw.ElapsedMilliseconds.ToString());
                       

            sw.Reset();
            json = muj1.SerializeToJsonString();
            sw.Start();
            for (int i = 0; i < 100000; i++)
            {
                json = muj1.SerializeToJsonString();
            }
            sw.Stop();
            Console.WriteLine("SernaJson: " + sw.ElapsedMilliseconds.ToString());


            //sw.Reset();
            ////json = JsonConvert.SerializeObject(muj1);
            ////sw.Start();
            ////for (int i = 0; i < 100000; i++)
            ////{
            ////    json = JsonConvert.SerializeObject(muj1);
            ////}
            ////sw.Stop();
            ////Console.WriteLine("JSON.NET: " + sw.ElapsedMilliseconds.ToString());

            //sw.Reset();
            //json = ServiceStack.Text.JsonSerializer.SerializeToString(muj1);
            //sw.Start();
            //for (int i = 0; i < 100000; i++)
            //{
            //    json = ServiceStack.Text.JsonSerializer.SerializeToString(muj1);
            //}
            //sw.Stop();
            //Console.WriteLine("ServiceStack: " + sw.ElapsedMilliseconds.ToString());



            sw.Reset();
            json = Jil.JSON.Serialize(muj1);
            sw.Start();
            for (int i = 0; i < 100000; i++)
            {
                json = Jil.JSON.Serialize(muj1);
            }
            sw.Stop();
            Console.WriteLine("JIL: " + sw.ElapsedMilliseconds.ToString());


            sw.Reset();
            NetJSON.NetJSON.IncludeFields = true;
            json = NetJSON.NetJSON.Serialize(muj1);
            sw.Start();
            for (int i = 0; i < 100000; i++)
            {
                json = NetJSON.NetJSON.Serialize(muj1);
            }
            sw.Stop();
            Console.WriteLine("NetJSON: " + sw.ElapsedMilliseconds.ToString());



            //sw.Reset();
            //btJson = muj1.SerializeProtobuf();
            //sw.Start();
            //for (int i = 0; i < 100000; i++)
            //{
            //    //btJson = muj1.SerializeProtobuf();
            //    desMuj1 = btJson.DeserializeProtobuf<MccUserData>();
            //}
            //sw.Stop();
            //Console.WriteLine("Protobuf: " + sw.ElapsedMilliseconds.ToString());


            //sw.Reset();

            //MsgPack.ObjectPacker dMsgPack = new MsgPack.ObjectPacker();
            //btJson = dMsgPack.Pack(muj1);

            //sw.Start();
            //for (int i = 0; i < 100000; i++)
            //{
            //    btJson = dMsgPack.Pack(muj1);
            //   // desMuj1 = dMsgPack.Unpack <MccUserData>(btJson,0,btJson.Length);
            //}
            //sw.Stop();
            //Console.WriteLine("MsgPack: " + sw.ElapsedMilliseconds.ToString());



            //sw.Reset();
            //var msgpackCliSerializer = MsgPack.Serialization.MessagePackSerializer.Get<MccUserData>();
            //btJson = msgpackCliSerializer.PackSingleObject(muj1);

            //sw.Start();
            //for (int i = 0; i < 100000; i++)
            //{
            //    //btJson = msgpackCliSerializer.PackSingleObject(muj1);
            //    desMuj1 = msgpackCliSerializer.UnpackSingleObject(btJson);
            //}
            //sw.Stop();
            //Console.WriteLine("MsgPackCLI: " + sw.ElapsedMilliseconds.ToString());

            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine("--------------------------------------------------");

        }




        public class DeTest
        {
            public DeTest()
            {
                //IntTest = 12;
                //tp = new TType1();
            }

            public int IntTest { get; set; }
            //public TType1 tp { get; set; }
            //public HashSet<int?> HS { get; set; }
            //public HashSet<List<int>> HLS { get; set; }
            public HashSet<Tuple<int,int,int>> HLT { get; set; }

        }

        /// <summary>
        /// Deserialize test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //string ser = System.IO.File.ReadAllText(@"d:\ser4.txt");
            DeTest dtn = new DeTest();
            dtn.IntTest = 15;

            //dtn.tp = new dtn.tp();
            //dtn.tp.P1 = 45589;

            //dtn.HS = new HashSet<int?> { 1, 45, 54, null, 898 };
            //dtn.HLS = new HashSet<List<int>>() { new List<int> { 1, 2, 3 }, new List<int> { 45, 45, 6 } };
            dtn.HLT = new HashSet<Tuple<int, int, int>>()
            {
                new Tuple<int,int,int>(1,1,1),
                new Tuple<int,int,int>(2,2,2)
            };

            var tr = Tuple.Create(1, 4, 5,"dsf");
            var trtype = tr.GetType();

            string ser = dtn.SerializeToJsonString();

            var det = ser.DeserializeFromJsonString<DeTest>();
            return;

        }

        
    }
}

